﻿
using Models.Common;
using Models.Device;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    public class ServiceDeviceI2C1004_SHT30WS
    {


        //public void gateWayExecuteCommand(object obj)
        //{
        //    MQReqModel<object> mQReqModel = (MQReqModel<object>)obj;
        //    CommandLogModel commandLogModel = mQReqModel.toGatewayData;
        //    ModelSht30Info modelSht30Info = new ModelSht30Info();
        //    ModelReceverData modelReceverData = new ModelReceverData();
        //    ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
        //    ModelCommandAgreementConent modelCommandAgreementConent = UtilsObject.DynamicToEntity<ModelCommandAgreementConent>(modelCommandAgreement.content);

        //    try
        //    {
        //        List<ModelCommandResult> results = new List<ModelCommandResult>();
        //        ModelCommandResult result = new ModelCommandResult();


        //        if (commandLogModel.agreementType == EnumAgreementType.I2C)
        //        {
        //            ModelCommandAgreementParameter modelI2C = modelCommandAgreement.agreementParameter;
        //            modelSht30Info = new Sensor_SHT30().getTempAndHum(modelI2C.busId);
        //            // modelReceverData = new ModelReceverData();
        //            //  modelReceverData.humidity = modelSht30Info.humidity;
        //            // modelReceverData.temperature = modelSht30Info.temperature;

        //            result = new ModelCommandResult();
        //            result.name = "湿度";
        //            result.key = "humidity";
        //            result.value = modelSht30Info.humidity.ToString("0.0");
        //            results.Add(result);

        //            result = new ModelCommandResult();
        //            result.name = "温度";
        //            result.key = "temperature";
        //            result.value = modelSht30Info.temperature.ToString("0.0");
        //            results.Add(result);

        //            modelReceverData.commandResults = results;
        //            mQReqModel.respCode = ResponseCode.SUCCESS;
        //            mQReqModel.respMsg = MQEventEnum.Event_State_OK;
        //            mQReqModel.toServerData = modelReceverData;
        //            new ServiceRabbitMQ_DeviceHelper().sendMsg(JsonConvert.SerializeObject(mQReqModel));

        //            Console.WriteLine("采集sht30" + JsonConvert.SerializeObject(mQReqModel));
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        //new GatewayFileHelper().log_WriteMessageAppend("error", "" + ex.ToString());
        //        Console.WriteLine(ex.ToString());
        //    }

        //}




        //回调获获状态,为了避免误差，查状态和执行，都返回了 当前开关状态
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData receverData = UtilsObject.DynamicToEntity<ModelReceverData>(httpReqModel.toServerData);
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;


                //更新设备状态，设备状态
                //string sql = "update iot_device set d_live_state=" + httpReqModel.respCode + ",d_live_state_set_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',d_live_sate_remark =@remark     where d_guid='" + httpReqModel.gatewayDeviceGuid + "'  ";
                // new UtilsORM.MySqlYihongIot().Db.Ado.ExecuteCommand(sql, new { remark = httpReqModel.respMsg });

                //new ServiceCloudDeviceLiveState().setDeviceLiveState(httpReqModel.gatewayDeviceGuid, int.Parse(httpReqModel.respCode), httpReqModel.respMsg);

                List<ModelCommandResult> commandResults = receverData.commandResults;
                ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                //写日志
                foreach (ModelCommandResult result in commandResults)
                {

                    log.writeSensorlog(commandLogModel.d_guid, result.key, result.value, result.name);
                }
                //  log.writeSensorlog(commandLogModel.d_guid, "humidity", modelSht30Info.humidity.ToString(), "湿度");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
